
#ifndef _ENV_H__
#define _ENV_H__

#include <stdint.h>

#include "BitDefine.h"


#define ENV_CH_485P			0x01
#define ENV_CH_485A			0x08
#define ENV_CH_KB			0x02
#define ENV_CH_NET			0x04

#define ENV_FIRST_CPU		0x00
#define ENV_FIRST_MF		0x01

#define ENV_InfoOpen_Card		0x01
#define ENV_InfoOpen_ChinaID	0x02

#define ENV_WorkMode_Terminal	0x00
#define ENV_WorkMode_Reader		0x01

#define ENV_LightMode_Normal	0x00
#define ENV_LightMode_Pause		0x01
#define ENV_LightMode_Senser	0x02

#define ENV_Qr_WorkMode_Bridges 		0
#define ENV_Qr_WorkMode_HNEncrypt			1
#define ENV_Qr_WorkMode_ZKEncrypt		2


#define ENV_Custom_MF_CheckKey		Bit(0)
#define ENV_Custom_CPU_CheckKey		Bit(1)
#define ENV_Custom_CheckCode		Bit(2)

#define ENV_Func_CPU	Bit(0)
#define ENV_Func_MF		Bit(1)
#define ENV_Func_SFZ	Bit(2)
#define ENV_Func_15693	Bit(3)

#define Flag_IsSet(v, f)		(((v)&(f)) == (f))
#define Flag_Set(v, f)			do{(v) |= (f);}while(0)
#define Flag_Clear(v, f)		do{(v) &= ~(f);}while(0)

#pragma pack(1)


typedef struct ConfigCardParamTag
{
	uint8_t CpuKey[16]; 							//16字节 0 -15 
	uint8_t CpuEfId;								//1字节 16
	uint8_t AlarmFlag;								//1字节 17
	uint8_t MfKey[6];								//6字节 [18]-[23]
	uint8_t MfBlock;								//1字节 [24]
	uint8_t MfOffset;								//1字节-[25]
	uint8_t Mode;									//1字节-[26]自定义卡选择
	uint8_t Wg; 									//1字节-[27]
	uint8_t WgParity;								//校验和 1字节 [28]
	uint8_t WgPulse[2]; 							//脉冲宽度[29],脉冲间隙[30]S
	uint8_t WgReverse;								//[31]	0正序输出 1 反序输出
	uint16_t CpuDfId;								//2字节[32]-[33]
	uint8_t rdSupport[2];						//2字节 [34]-[35] 自定义卡选择区

	//Glz 2018.07.07 add for 兼容读头统一定义的，全部加进来
	uint8_t SQCard_Check_sum;						//前面配置卡校验值[36]
	uint8_t card_15693_is_high; 					//15693  前四  后 四	0 前4，1 后 四[37]
	uint8_t card_Iclass_is_high;					//Iclass   前四  后 四	 0 前 4，1后4[38]
	uint8_t card_Felica_is_high;					//Felica  前四  后 四	0 前 4，1后4[39]
	uint8_t rdMifareFirst;		//CPU 卡和MF卡优先选择, 0 CPU卡优先；1 MF卡优先 ， 默认CPU卡优先 [40]

	//2D
	uint8_t InfoOpenDoor;							//??
	uint8_t CpuKeyId;								//??

	//future
	uint8_t NULL1;
	uint8_t NULL2;
	uint8_t NULL3;

	//CA 
	uint8_t ContentStartPosition;					//Start position of read file that in cpu card.
	uint8_t ContentLength;							//offset
	uint8_t Customer_Code[4];						//0: without customer code ;   !0: with customer code.
	uint8_t Customer_CPU_Position;
	uint8_t Customer_M1_Position;
	uint8_t Snr4_StartPos;
	uint8_t Snr4_Len;
	uint8_t Snr8_StartPos;
	uint8_t Snr8_Len;
	uint8_t Options_select; 						//Bit0: mifare card KeyCheck enable;			Bit1: CPU card KeyCheck enable;    Bit2:  Customer code select function.
} ConfigCardParam_T;

typedef struct EnvDataTag{
	uint8_t version;			//参数版本
	uint8_t inited;				//参数初始化标志
	uint8_t devAddr;			//设备485地址
	uint8_t qrDID;				//二维码门ID
	uint8_t qrPwdLen;			//二维码秘钥长度
	uint8_t qrPwd[24];			//二维码秘钥
	uint8_t qrWorkMode;			//二维码工作模式
	uint8_t qrValidity;			//二维码有效时间(时间误差补偿)
	uint8_t wg;					//韦根长度:26,34等
	uint8_t doorUnlockTime;		//开门状态保持时长
	uint8_t qrDMode;			//补光灯工作模式
	uint8_t hidFormat;			//HID键盘输出格式：HEX，DEC等
	uint8_t wgPulseWidth[2];	//韦根脉宽
	uint8_t wgParityFlag;		//韦根奇偶校验使能
	uint8_t rdMode;				//读卡模式
	uint8_t channelEnable;		//传输通道使能
	uint8_t out485Order;		//485输出卡号顺序（废弃）
	uint8_t wgReverse;			//韦根正反序配置
	//读头参数
	uint8_t rdSupport[2];		//自定义读卡模式标志
	uint16_t rdCpuDfId;			//读CPU卡内容时使用的DF ID
	uint16_t rdCpuEfId;			//读CPU卡内容时使用的EF ID
	uint8_t rdCpuKey[16];		//读CPU卡内容时使用的秘钥
	uint8_t rdMfBlock;			//读MF卡内容时使用的块号
	uint8_t rdMfOffset;			//读MF卡内容时使用的偏移量
	uint8_t rdMfKey[6];			//读MF卡内容时使用的秘钥
	uint8_t rdErrorWarnning;	//认证错误警告使能
	//网络参数
	uint8_t netMAC[6];		//本地MAC地址
	uint8_t netClientIp[4];		//本地IP
	uint16_t netClientPort;		//本地端口
	uint8_t netServerIp[4];		//服务器IP
	uint16_t netServerPort;		//服务器端口
	uint8_t netGateWay[4];		//网关
	uint8_t netSubMask[4];		//子网掩码

	uint8_t qrDidOut;		//did输出
	uint8_t netHeartbeat;	//自动连接间隔时间,单位100ms
	uint8_t devMode;		//一体机模式和读头模式
	uint8_t rdMifareFirst;	//复合卡时，0：CPU优先，1：Mifare优先。
	uint8_t rdAutoReport;	//485或网络主动上传
	uint8_t rdInfoOpenDoor;	//读内容成功开门: 0x01 - 卡内容，0x02 - 身份证内容
	uint8_t rdCpuKeyId;		//CPU卡外部认证KeyID
	
	//ao
	uint8_t ContentStartPosition;		//Start position of read file that in cpu card.
	uint8_t ContentLength;		//offset
	uint8_t Customer_Code[4];			//0: without customer code ;   !0: with customer code.
	uint8_t Customer_CPU_Position;
	uint8_t Customer_M1_Position;	
	uint8_t Snr4_StartPos;
	uint8_t Snr4_Len;
	uint8_t Snr8_StartPos;
	uint8_t Snr8_Len;	
	uint8_t Options_select;					//Bit0: mifare card KeyCheck enable;			Bit1: CPU card KeyCheck enable;    Bit2:  Customer code select function.


	uint8_t backlight;
	uint32_t baudrate;
	uint8_t qrReverse;		//二维码反序输出
	uint8_t devSnr[6];		//设备序列号	
}EnvData_T;

#pragma pack()

#ifndef ENV_C__
extern EnvData_T envData;
#endif

void Env_Init(void);
void Env_Service(void);
void Env_Update(void);
void Env_Reset(void);
void Env_Lock(void);
void Env_Unlock(void);

#endif



